package com.bsoft.gol.hcb.service;

import com.bsoft.gol.hcb.dao.PcnCmsActionDAO;
import ctd.controller.exception.ControllerException;
import ctd.net.rpc.Client;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.util.CommonUtils;
import hcn.util.RpcUserToken;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.bean.sign.PcnCmsActionQo;
import pcn.bean.sign.PcnHealthActivitiesQo;
import pcn.bean.sign.PcnHealthNewsQo;
import pcn.sign.PcnCmsAction;
import pcn.sign.PcnHealthActivities;
import pcn.sign.PcnHealthNews;
import pcn.vo.sign.PcnHealthActivitiesAppVO;
import pcn.vo.sign.PcnHealthActivitiesVO;
import pcn.vo.sign.PcnHealthNewsAppVo;
import pcn.vo.sign.PcnHealthNewsVo;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 查询用户收藏接口
 */
@SsdevService("pcnCmsService")
public class PcnCmsService {
    @Autowired
    private PcnCmsActionDAO pcnCmsActionDAO;


    /**
     * 获取用户的资讯收藏信息
     *
     * @param qo
     * @return
     * @throws Exception
     */
    @RpcService
    public PcnHealthNewsVo getUserMarkInfoNews(PcnHealthNewsQo qo) throws Exception {
        String uid = RpcUserToken.getUserId();
        qo.setUid(uid);
        PcnHealthNewsVo phv = pcnCmsActionDAO.getUserMarkInfo(qo,"news");
        if(phv!=null && phv.getList()!=null) {
            List<PcnHealthNewsAppVo> list = phv.getList();
            for (PcnHealthNewsAppVo pcnHealthNewsAppVo : list) {
                try {
                    PcnHealthNews pcnHealthNews = (PcnHealthNews) Client.rpcInvoke("pcn.pcnHealthNewsService", "getPcnHealthNews", pcnHealthNewsAppVo.getId());
                    if(pcnHealthNews!=null) {
                        pcnHealthNewsAppVo.setCategory(pcnHealthNews.getCategory());
                        pcnHealthNewsAppVo.setId(pcnHealthNews.getId());
                        pcnHealthNewsAppVo.setReadCount(pcnHealthNews.getReadCount());
                        pcnHealthNewsAppVo.setTitle(pcnHealthNews.getTitle());
                        pcnHealthNewsAppVo.setSource(pcnHealthNews.getSource());
                    }
                } catch (Exception e) {
                    throw new ControllerException("查询咨询信息失败");
                }
            }
        }
        return phv;
    }


    /**
     * 获取用户的活动收藏信息
     *
     * @param qo
     * @return
     * @throws Exception
     */
    @RpcService
    public PcnHealthActivitiesAppVO getUserMarkInfoActivities(PcnHealthActivitiesQo qo) throws Exception {
        String uid = RpcUserToken.getUserId();
        qo.setUid(uid);
        PcnHealthActivitiesAppVO phv = pcnCmsActionDAO.getUserMarkInfoActivities(qo,"activity");
        if(phv!=null && phv.getList()!=null) {
            List<PcnHealthActivitiesVO> list = phv.getList();
            for (PcnHealthActivitiesVO pcnHealthActivitiesVO : list) {
                try {
                    PcnHealthActivities pcnHealthActivities = (PcnHealthActivities) Client.rpcInvoke("pcn.pcnHealthActivitiesService", "detailInfo", pcnHealthActivitiesVO.getId());
                    if(pcnHealthActivities!=null) {
                        pcnHealthActivitiesVO.setTitle(pcnHealthActivities.getTitle());
                        pcnHealthActivitiesVO.setAtype(pcnHealthActivities.getAtype());
                        pcnHealthActivitiesVO.setId(pcnHealthActivities.getId());
                        pcnHealthActivitiesVO.setReadCount(pcnHealthActivities.getReadCount());
                        pcnHealthActivitiesVO.setSpeakers(pcnHealthActivities.getSpeakers());
                        pcnHealthActivitiesVO.setJobTitle(pcnHealthActivities.getJobTitle());
                        pcnHealthActivitiesVO.setCreateUser(pcnHealthActivities.getCreateUser());
                        pcnHealthActivitiesVO.setCreateDt(pcnHealthActivities.getCreateDt());
                    }
                } catch (Exception e) {
                    throw new ControllerException("查询活动信息失败");
                }
            }
            filterHealthActivities(list,qo.getAtype());
        }
        return phv;
    }

    /**
     * 数据过滤
     * @param list
     * @param atype
     */
    private static void filterHealthActivities(List<PcnHealthActivitiesVO> list,String atype){
        Iterator<PcnHealthActivitiesVO> sListIterator = list.iterator();
        while (sListIterator.hasNext()) {
            PcnHealthActivitiesVO pcnHealthActivitiesVO = sListIterator.next();
            if (CommonUtils.isNotEmpty(atype)&&!atype.equals(pcnHealthActivitiesVO.getAtype())) {
                sListIterator.remove();
            }
        }
    }

    @RpcService
    public Map<String, Object> detail(PcnCmsActionQo qo) {
        List<PcnCmsAction> list = pcnCmsActionDAO.findListByUserIdAndBusinessObjId(RpcUserToken.getUserId(), qo.getBusinessObjId(), qo.getBusinessType());
        Map<String, Object> map = new HashedMap();
        if (CollectionUtils.isNotEmpty(list)) {
            for (PcnCmsAction action : list) {
                map.put(action.getAction(), true);
            }
        }
        return map;
    }
    @RpcService
    public void save(PcnCmsAction action) {
        pcnCmsActionDAO.save(action);

    }
    @RpcService
    public void remove(Integer id) {
        pcnCmsActionDAO.remove(id);

    }
    @RpcService
    public void submit(PcnCmsAction action) {
        action.setUid(RpcUserToken.getUserId());
        PcnCmsAction dbRecord = pcnCmsActionDAO.getPcnCmsActionByUserIdAndBusinessObjId(action.getUid(), action.getBusinessObjId(), action.getAction(), action.getBusinessType());
//        CmsActionHandler handler = getHandler(action.getBusinessType(), action.getAction());
        if (dbRecord != null) {
            action.setId(dbRecord.getId());
        }

//        if (handler.doAction(dbRecord != null, action)) {
            pcnCmsActionDAO.save(action);
//        }
    }

    @RpcService
    public void cancel(PcnCmsAction action) {
        action.setUid(RpcUserToken.getUserId());
        PcnCmsAction dbRecord = pcnCmsActionDAO.getPcnCmsActionByUserIdAndBusinessObjId(action.getUid(), action.getBusinessObjId(), action.getAction(), action.getBusinessType());
//        CmsActionHandler handler = getHandler(action.getBusinessType(), action.getAction());
//        action.setId(dbRecord.getId());
//        handler.doCancelAction(action);
        pcnCmsActionDAO.remove(dbRecord.getId());
    }

//    private CmsActionHandler getHandler(String businessType, String action) {
//        CmsActionHandler handler = CmsActionFactory.getHandler(businessType, action);
//        Assert.notNull(handler, String.format("未定义动作处理器,businessType=%s,action=%s", businessType, action));
//        return handler;
//    }
    @RpcService
    public PcnCmsAction get(PcnCmsAction action) {
        return pcnCmsActionDAO.getPcnCmsActionByUserIdAndBusinessObjId(action.getUid(), action.getBusinessObjId(), action.getAction(), action.getBusinessType());
    }

    public void setPcnCmsActionDAO(PcnCmsActionDAO pcnCmsActionDAO) {
        this.pcnCmsActionDAO = pcnCmsActionDAO;
    }
}
